<template>
  <view class="message-page">
   
    <!-- 消息内容区域 -->
    <scroll-view scroll-y class="message-scroll">
      <!-- 系统消息 -->
      <view class="message-list" v-if="currentTab === 0">
        <view class="message-group" v-for="(group, groupIndex) in systemMessages" :key="groupIndex">
          <view class="message-date">{{ group.date ||'2025-08-12' }}</view>
          <view 
            class="message-item system-item" 
            v-for="(msg, msgIndex) in group.messages" 
            :key="msgIndex"
            :class="{ unread: msg.unread }"
            @click="handleSystemMessageClick(msg)"
          >
            <view class="system-icon-container">
              <uni-icons :type="msg.icon" size="24" :color="msg.iconColor"></uni-icons>
            </view>
            
            <view class="msg-content">
              <view class="msg-header">
                <text class="msg-sender">{{ msg.sender }}</text>
                <text class="msg-time">{{ msg.time }}</text>
              </view>
              
              <view class="msg-body">
                <text class="msg-text system-msg">{{ msg.content }}</text>
              </view>
              
              <view class="system-extra" v-if="msg.buttonText">
                <button class="system-btn" @click.stop="handleSystemButtonClick(msg)">{{ msg.buttonText }}</button>
              </view>
            </view>
            
            <view class="msg-unread-dot" v-if="msg.unread"></view>
          </view>
        </view>
        
        <view class="no-messages" v-if="systemMessages.length === 0">
          <uni-icons type="notice" size="60" color="#C9CDD4"></uni-icons>
          <text class="no-messages-text">暂无系统消息</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
     
      currentTab: 0, // 当前选中的标签
      
      // 全部消息（按日期分组）
      allMessages: [
        {
          date: '今天',
          messages: [
            {
              id: 1,
              type: 'like',
              typeColor: '#FF4D4F',
              sender: '飞手小王',
              avatar: 'https://picsum.photos/id/1025/100/100',
              action: '赞了你的话题',
              content: '大疆Mavic 3 Pro使用一个月体验分享',
              time: '10:24',
              unread: true,
              targetId: 1001
            },
            {
              id: 2,
              type: 'comment',
              typeColor: '#165DFF',
              sender: '无人机爱好者',
              avatar: 'https://picsum.photos/id/1074/100/100',
              action: '评论了你的话题',
              content: '请问这款无人机的抗风能力怎么样？我经常在海边飞',
              time: '09:15',
              unread: true,
              targetId: 1001
            },
            {
              id: 3,
              type: 'private',
              typeColor: '#00B42A',
              sender: '航拍达人',
              avatar: 'https://picsum.photos/id/1062/100/100',
              content: '你好，想请教一下Mavic 3 Pro的拍摄参数设置',
              time: '08:30',
              unread: true,
              chatId: 501
            }
          ]
        },
        {
          date: '昨天',
          messages: [
            {
              id: 4,
              type: 'reply',
              typeColor: '#722ED1',
              sender: '飞手小李',
              avatar: 'https://picsum.photos/id/1012/100/100',
              action: '回复了你的评论',
              content: '抗风能力不错，6级风以内没问题',
              time: '18:45',
              unread: true,
              targetId: 1001,
              commentId: 205
            },
            {
              id: 5,
              type: 'system',
              typeColor: '#FF7D00',
              sender: '系统通知',
              icon: 'notice',
              iconColor: '#FF7D00',
              content: '你的话题《大疆Mavic 3 Pro使用体验》已被推荐至首页',
              time: '15:20',
              unread: false
            }
          ]
        },
        {
          date: '3天前',
          messages: [
            {
              id: 6,
              type: 'follow',
              typeColor: '#13C2C2',
              sender: '新手玩家',
              avatar: 'https://picsum.photos/id/1027/100/100',
              action: '关注了你',
              content: '',
              time: '16:30',
              unread: false
            },
            {
              id: 7,
              type: 'like',
              typeColor: '#FF4D4F',
              sender: '摄影爱好者',
              avatar: 'https://picsum.photos/id/1083/100/100',
              action: '赞了你的评论',
              content: '我也买了这款，确实不错，尤其是夜景模式提升明显',
              time: '14:15',
              unread: false,
              targetId: 1001,
              commentId: 189
            }
          ]
        }
      ],
      
      // 互动消息（点赞、评论、回复、关注）
      interactionMessages: [
        {
          date: '今天',
          messages: [
            {
              id: 1,
              type: 'like',
              typeColor: '#FF4D4F',
              sender: '飞手小王',
              avatar: 'https://picsum.photos/id/1025/100/100',
              action: '赞了你的话题',
              content: '大疆Mavic 3 Pro使用一个月体验分享',
              time: '10:24',
              unread: true,
              targetId: 1001
            },
            {
              id: 2,
              type: 'comment',
              typeColor: '#165DFF',
              sender: '无人机爱好者',
              avatar: 'https://picsum.photos/id/1074/100/100',
              action: '评论了你的话题',
              content: '请问这款无人机的抗风能力怎么样？我经常在海边飞',
              time: '09:15',
              unread: true,
              targetId: 1001
            }
          ]
        },
        {
          date: '昨天',
          messages: [
            {
              id: 4,
              type: 'reply',
              typeColor: '#722ED1',
              sender: '飞手小李',
              avatar: 'https://picsum.photos/id/1012/100/100',
              action: '回复了你的评论',
              content: '抗风能力不错，6级风以内没问题',
              time: '18:45',
              unread: true,
              targetId: 1001,
              commentId: 205
            }
          ]
        },
        {
          date: '3天前',
          messages: [
            {
              id: 6,
              type: 'follow',
              typeColor: '#13C2C2',
              sender: '新手玩家',
              avatar: 'https://picsum.photos/id/1027/100/100',
              action: '关注了你',
              content: '',
              time: '16:30',
              unread: false
            },
            {
              id: 7,
              type: 'like',
              typeColor: '#FF4D4F',
              sender: '摄影爱好者',
              avatar: 'https://picsum.photos/id/1083/100/100',
              action: '赞了你的评论',
              content: '我也买了这款，确实不错，尤其是夜景模式提升明显',
              time: '14:15',
              unread: false,
              targetId: 1001,
              commentId: 189
            }
          ]
        }
      ],
      
      // 私信消息
      privateMessages: [
        {
          id: 501,
          name: '航拍达人',
          avatar: 'https://picsum.photos/id/1062/100/100',
          lastTime: '08:30',
          lastMessage: {
            content: '你好，想请教一下Mavic 3 Pro的拍摄参数设置',
            isAt: false
          },
          unreadCount: 1,
          isTop: true
        },
        {
          id: 502,
          name: '飞手社区官方',
          avatar: 'https://picsum.photos/id/1005/100/100',
          lastTime: '昨天 10:20',
          lastMessage: {
            content: '恭喜你获得"优秀分享者"称号，奖励已发放',
            isAt: false
          },
          unreadCount: 0,
          isTop: false
        },
        {
          id: 503,
          name: '新手玩家',
          avatar: 'https://picsum.photos/id/1027/100/100',
          lastTime: '11月10日',
          lastMessage: {
            content: '@你 请问这个问题你解决了吗？',
            isAt: true
          },
          unreadCount: 2,
          isTop: false
        }
      ],
      
      // 系统消息
      systemMessages: [
        {
          date: '昨天',
          messages: [
            {
              id: 5,
              type: 'system',
              sender: '系统通知',
              icon: 'notice',
              iconColor: '#FF7D00',
              content: '你的话题《大疆Mavic 3 Pro使用体验》已被推荐至首页',
              time: '15:20',
              unread: false
            }
          ]
        },
        {
          date: '11月10日',
          messages: [
            {
              id: 8,
              type: 'system',
              sender: '社区公告',
              icon: 'news',
              iconColor: '#165DFF',
              content: '【活动通知】11月20日将举办无人机航拍大赛，点击查看详情',
              time: '09:30',
              unread: false,
              buttonText: '查看详情',
              actionUrl: '/pages/activity/detail?id=301'
            }
          ]
        },
        {
          date: '11月5日',
          messages: [
            {
              id: 9,
              type: 'system',
              sender: '系统通知',
              icon: 'warning',
              iconColor: '#FF4D4F',
              content: '你的一条评论因违规已被删除，请遵守社区规范',
              time: '16:45',
              unread: false
            }
          ]
        }
      ]
    };
  },
  
  methods: {
    // 切换消息标签
    switchTab(index) {
      if (this.currentTab === index) return;
      this.currentTab = index;
      
      // 切换到对应标签时标记为已读
      if (this.messageTabs[index].unread > 0) {
        this.messageTabs[index].unread = 0;
        
        // 如果切换到全部标签，需要重新计算未读总数
        if (index === 0) {
          let totalUnread = 0;
          for (let i = 1; i < this.messageTabs.length; i++) {
            totalUnread += this.messageTabs[i].unread;
          }
          this.messageTabs[0].unread = totalUnread;
        }
      }
    },
    
    // 处理消息点击
    handleMessageClick(msg) {
      // 标记为已读
      if (msg.unread) {
        msg.unread = false;
        
        // 更新未读计数
        this.updateUnreadCounts();
      }
      
      // 根据消息类型跳转到相应页面
      switch(msg.type) {
        case 'like':
        case 'comment':
        case 'reply':
          // 跳转到话题详情
          uni.navigateTo({
            url: `/pages/community/topic-detail?id=${msg.targetId}`
          });
          break;
        case 'follow':
          // 跳转到对方主页
          uni.navigateTo({
            url: `/pages/profile/other-profile?id=${msg.senderId}`
          });
          break;
        case 'private':
          // 跳转到聊天页面
          this.gotoChat({ id: msg.chatId });
          break;
      }
    },
    
    // 处理系统消息点击
    handleSystemMessageClick(msg) {
      // 标记为已读
      if (msg.unread) {
        msg.unread = false;
        
        // 更新未读计数
        this.updateUnreadCounts();
      }
    },
    
    // 处理系统消息按钮点击
    handleSystemButtonClick(msg) {
      if (msg.actionUrl) {
        uni.navigateTo({
          url: msg.actionUrl
        });
      }
    },
    
    // 跳转到聊天页面
    gotoChat(chat) {
      // 标记为已读
      if (chat.unreadCount > 0) {
        chat.unreadCount = 0;
        
        // 更新未读计数
        this.updateUnreadCounts();
      }
      
      uni.navigateTo({
        url: `/pages/message/chat?id=${chat.id}&name=${encodeURIComponent(chat.name)}&avatar=${encodeURIComponent(chat.avatar)}`
      });
    },
    
    // 跳转到联系人列表
    gotoContactList() {
      uni.navigateTo({
        url: '/pages/message/contacts'
      });
    },
    
    // 跳转到消息设置
    gotoMessageSettings() {
      uni.navigateTo({
        url: '/pages/message/settings'
      });
    },
    
    // 更新未读消息计数
    updateUnreadCounts() {
      // 计算互动消息未读数量
      let interactionUnread = 0;
      this.interactionMessages.forEach(group => {
        group.messages.forEach(msg => {
          if (msg.unread) interactionUnread++;
        });
      });
      this.messageTabs[1].unread = interactionUnread;
      
      // 计算私信未读数量
      let privateUnread = 0;
      this.privateMessages.forEach(chat => {
        privateUnread += chat.unreadCount;
      });
      this.messageTabs[2].unread = privateUnread;
      
      // 计算系统消息未读数量
      let systemUnread = 0;
      this.systemMessages.forEach(group => {
        group.messages.forEach(msg => {
          if (msg.unread) systemUnread++;
        });
      });
      this.messageTabs[3].unread = systemUnread;
      
      // 计算全部消息未读数量
      this.messageTabs[0].unread = interactionUnread + privateUnread + systemUnread;
    },
    
    // 聚焦评论输入框
    focusComment() {
      this.$refs.commentInput.focus();
    }
  }
};
</script>

<style scoped lang="scss">
// 颜色变量
$primary: #165DFF;
$primary-light: #E8F3FF;
$danger: #FF4D4F;
$warning: #FF7D00;
$success: #00B42A;
$purple: #722ED1;
$cyan: #13C2C2;
$text-primary: #1D2129;
$text-secondary: #4E5969;
$text-tertiary: #86909C;
$bg-light: #F5F7FA;
$bg-white: #FFFFFF;
$bg-gray: #F2F3F5;
$border-light: #E5E6EB;
$shadow-light: 0 2px 10px rgba(0, 0, 0, 0.05);

.message-page {
  background-color: $bg-light;
  min-height: 100vh;
  padding-top: var(--status-bar-height);
}

// 导航栏
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 16px;
  background-color: $bg-white;
  border-bottom: 1px solid $border-light;
  
  .nav-title {
    font-size: 18px;
    font-weight: 600;
    color: $text-primary;
  }
  
  .setting-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: $text-secondary;
    
    &:active {
      background-color: $bg-light;
    }
  }
}

// 消息分类标签
.message-tabs {
  display: flex;
  background-color: $bg-white;
  border-bottom: 1px solid $border-light;
  
  .tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 48px;
    position: relative;
    font-size: 15px;
    color: $text-secondary;
    
    &.active {
      color: $primary;
      font-weight: 500;
    }
    
    .tab-badge {
      position: absolute;
      top: 8px;
      right: 30%;
      min-width: 18px;
      height: 18px;
      background-color: $danger;
      color: white;
      font-size: 12px;
      border-radius: 9px;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 4px;
    }
    
    .tab-indicator {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 3px;
      background-color: $primary;
    }
  }
}

// 消息滚动区域
.message-scroll {
  height: calc(100vh - var(--status-bar-height) - 48px - 48px);
}

// 消息列表
.message-list {
  padding-bottom: 10px;
  
  .message-group {
    margin-bottom: 10px;
    
    .message-date {
      padding: 8px 16px;
      font-size: 12px;
      color: $text-tertiary;
      background-color: $bg-light;
    }
    
    .message-item {
      display: flex;
      align-items: flex-start;
      padding: 12px 16px;
      background-color: $bg-white;
      border-bottom: 1px solid $border-light;
      
      &:last-child {
        border-bottom: none;
      }
      
      &.unread {
        background-color: rgba(22, 93, 255, 0.03);
      }
      
      .msg-avatar-container {
        position: relative;
        margin-right: 12px;
        
        .msg-avatar {
          width: 44px;
          height: 44px;
          border-radius: 8px;
          object-fit: cover;
        }
        
        .msg-type-indicator {
          position: absolute;
          bottom: 0;
          right: 0;
          width: 14px;
          height: 14px;
          border-radius: 50%;
          border: 2px solid white;
        }
      }
      
      .msg-content {
        flex: 1;
        min-width: 0;
        
        .msg-header {
          display: flex;
          justify-content: space-between;
          margin-bottom: 4px;
          
          .msg-sender {
            font-size: 15px;
            font-weight: 500;
            color: $text-primary;
          }
          
          .msg-time {
            font-size: 12px;
            color: $text-tertiary;
            white-space: nowrap;
          }
        }
        
        .msg-body {
          display: flex;
          align-items: flex-start;
          
          .msg-action {
            font-size: 14px;
            color: $text-secondary;
            margin-right: 4px;
          }
          
          .msg-preview {
            font-size: 14px;
            color: $text-tertiary;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            flex: 1;
          }
          
          .msg-text {
            font-size: 14px;
            line-height: 1.5;
          }
          
          .system-msg {
            color: $text-secondary;
          }
        }
        
        .msg-extra {
          margin-top: 6px;
          
          .msg-image {
            width: 48px;
            height: 48px;
            border-radius: 4px;
            object-fit: cover;
          }
        }
      }
      
      .msg-unread-dot {
        width: 8px;
        height: 8px;
        background-color: $primary;
        border-radius: 50%;
        margin-top: 18px;
      }
    }
    
    // 系统消息项
    .system-item {
      .system-icon-container {
        width: 44px;
        height: 44px;
        border-radius: 8px;
        background-color: $bg-light;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12px;
      }
      
      .system-extra {
        margin-top: 8px;
        
        .system-btn {
          background-color: $primary-light;
          color: $primary;
          border-radius: 4px;
          font-size: 13px;
          padding: 4px 12px;
        }
      }
    }
  }
  
  // 私信聊天项
  .chat-item {
    padding: 10px 16px;
    
    .chat-avatar {
      width: 50px;
      height: 50px;
      border-radius: 8px;
      object-fit: cover;
      margin-right: 12px;
    }
    
    .chat-content {
      flex: 1;
      min-width: 0;
      
      .chat-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 4px;
        
        .chat-name {
          font-size: 16px;
          font-weight: 500;
          color: $text-primary;
        }
        
        .chat-time {
          font-size: 12px;
          color: $text-tertiary;
          white-space: nowrap;
        }
      }
      
      .chat-last-message {
        display: flex;
        align-items: center;
        
        .at-icon {
          color: $primary;
          margin-right: 4px;
        }
        
        .last-message-text {
          font-size: 14px;
          color: $text-tertiary;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          flex: 1;
        }
      }
    }
    
    .chat-unread {
      min-width: 24px;
      height: 24px;
      background-color: $primary;
      color: white;
      font-size: 12px;
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 13px;
      padding: 0 6px;
    }
    
    &.top {
      background-color: rgba(22, 93, 255, 0.05);
    }
  }
  
  // 私信搜索框
  .message-search {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    background-color: $bg-white;
    border-bottom: 1px solid $border-light;
    
    .search-icon {
      color: $text-tertiary;
      margin-right: 8px;
    }
    
    .search-input {
      flex: 1;
      height: 36px;
      background-color: $bg-light;
      border-radius: 18px;
      padding: 0 12px;
      font-size: 14px;
      color: $text-primary;
      
      &::placeholder {
        color: $text-tertiary;
      }
    }
  }
  
  // 无消息状态
  .no-messages {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    color: $text-tertiary;
    
    .no-messages-text {
      font-size: 16px;
      margin: 20px 0;
    }
    
    .start-chat-btn {
      background-color: $primary;
      color: white;
      border-radius: 20px;
      padding: 8px 24px;
      font-size: 14px;
    }
  }
}
</style>
    